/**
 * Created by LC on 2019/11/12
 */
package com.hw.paltfrom.controller.doncutor;

import com.hw.paltfrom.pojo.HwBaseWorkflowstep;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.casemain.CountryCasemainServer;
import com.hw.paltfrom.service.doncutor.CityCommandTaskService;
import com.hw.paltfrom.service.doncutor.CommonWorkflowService;
import com.hw.paltfrom.service.doncutor.CountyCommandTaskService;
import com.hw.paltfrom.service.doncutor.StationCommandTaskServer;
import com.hw.paltfrom.service.supervisor.SupervisorManagerServer;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.StepInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 公共调用接口
 */
@RestController
@RequestMapping("common")
public class CommonWorkflowController {

	@Autowired
	private CityCommandTaskService cityCommandTaskService;// 市接口

	@Autowired
	private CountyCommandTaskService countyCommandTaskService;// 县接口

	@Autowired
	private StationCommandTaskServer stationCommandTaskServer;// 站接口

	@Autowired
	private CommonWorkflowService commonWorkflowService;

	@Autowired
	private SupervisorManagerServer supervisorManagerServer;

	@Autowired
	private CountryCasemainServer countryCasemainServer; // 执法监管县

	@RequestMapping("page")
	public Map<String, Object> queryCommonPage(HwBaseWorkflowstep baseWorkflowstep) {
		String workflowCode = baseWorkflowstep.getWorkflowCode();
		String workflowType = baseWorkflowstep.getWorkflowType();
		if (StringUtils.isBlank(workflowCode) || StringUtils.isBlank(workflowType)) {
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
		return commonWorkflowService.findPage(workflowCode, workflowType);
	}

	/**
	 * 根据所传数据调用不同接口
	 * 
	 * @param stepInfoVO
	 * @return
	 */
	@RequestMapping("functions")
	public Map<String, Object> invokeFunctions(@RequestBody StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkflowModule()) {
		case "01":
			return zhddTask(stepInfoVO);
		case "02":
			return zfjgTask(stepInfoVO);
		case "03":
			return jcddTask(stepInfoVO);
		case "04":
			return sbglTask(stepInfoVO);
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	// 查询时间轴
	@RequestMapping("scroll")
	public Map<String, Object> findTimeScroll(String taskId) {
		if (StringUtils.isBlank(taskId)) {
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
		return commonWorkflowService.findTimeScroll(taskId);
	}

	/**
	 * "01" for 指挥调度
	 *
	 * @param stepInfoVO
	 * @return json
	 */
	private Map<String, Object> zhddTask(StepInfoVO stepInfoVO) {
		// Flow_Code（异地复检YDFJ、移交YJ、市级举报及其他SJJB、县级创建举报及其他XJJB）
		switch (stepInfoVO.getWorkflowType()) {
		case "YDFJ":
			return ydfjTask(stepInfoVO);
		case "YJ":
			return yjTask(stepInfoVO);
		case "SJJB":
			return sjjbTask(stepInfoVO);
		case "XJJB":
			return xjjbTask(stepInfoVO);
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "YDFJ" for 异地复检
	 *
	 * @return
	 */
	private Map<String, Object> ydfjTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "YDFJ070":
			return countyCommandTaskService.theEnd(stepInfoVO);// 调用指挥调度的异地复检任务的完结操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "YJ" for 移交
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> yjTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "YJ030":
			return stationCommandTaskServer.presentation(stepInfoVO);// 调用指挥调度的移交任务的移交情况说明上报操作
		case "YJ050":
			return countyCommandTaskService.transferConfirmation(stepInfoVO);// 调用指挥调度的移交任务的移交确认操作
		case "YJ051":
			return countyCommandTaskService.transferConfirmation(stepInfoVO);// 调用指挥调度的移交任务的确认审核退回操作
		case "YJ060":
			return stationCommandTaskServer.submitDelivery(stepInfoVO);// 调用指挥调度的移交任务的移交单上报操作
		case "YJ070":
			return stationCommandTaskServer.confirmTheTransfer(stepInfoVO);// 调用指挥调度的移交任务的确认移交并提交完结操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "SJJB" for 市级举报
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> sjjbTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "SJJB050":
			return countyCommandTaskService.assignedConfirm(stepInfoVO);// 调用指挥调度的市级创建任务的确认操作
		case "SJJB051":
			return countyCommandTaskService.assignedConfirm(stepInfoVO);// 调用指挥调度的市级创建任务的确认审核退回操作
		case "SJJB070":
			return cityCommandTaskService.taskFinish(stepInfoVO);// 调用指挥调度的市级创建任务的完结操作
		case "SJJB071":
			return cityCommandTaskService.taskFinish(stepInfoVO);// 调用指挥调度的市级创建任务的完结审核退回操作;
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "XJJB" for 县级举报
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> xjjbTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "XJJB070":
			return countyCommandTaskService.assignedEnd(stepInfoVO);// 调用业务指挥调度的完结操作
		case "XJJB071":
			return countyCommandTaskService.assignedEnd(stepInfoVO);// 调用业务指挥调度的完结退回操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "02" for 执法监管
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> zfjgTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkflowType()) {
		case "SHZF":
			return shzfTask(stepInfoVO);// 调用散货执法
		case "BKJTZF":
			return bkjtzfTask(stepInfoVO);// 调用不可解体
		case "NCPZF":
			return ncpzfTask(stepInfoVO);// 调用农产品
		case "ZDFXZF":
			return zdfxzfTask(stepInfoVO);// 调用散货执法的完结操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "SHZF" for 散货执法
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> shzfTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "SHZF030":
			return countryCasemainServer.initiateAnAudit(stepInfoVO);// 调用散货执法的立案审核通过操作
		case "SHZF031":
			return countryCasemainServer.initiateAnAudit(stepInfoVO);// 调用散货执法的立案审核退回操作
		case "SHZF040":
			return countryCasemainServer.release(stepInfoVO);// 调用散货执法的上报放行单
		case "SHZF060":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用散货执法的完结操作
		case "SHZF061":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用散货执法的完结操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "BKJTZF" for 不可解体执法
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> bkjtzfTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "BKJTZF060":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用不可解体执法的完结操作
		case "BKJTZF061":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用不可解体执法的待完结退回操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "NCPZF" for 不可解体执法
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> ncpzfTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "NCPZF060":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用农产品执法的完结操作
		case "NCPZF061":
			return countryCasemainServer.auditEnd(stepInfoVO);// 调用农产品执法的完结退回操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "ZDFXZF" for 自动方向
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> zdfxzfTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "ZDFXZF060":
			return null;// 调用自动放行的完结操作
		case "ZDFXZF061":
			return null;// 调用自动放行的完结退回操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "03" for 检查督导
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> jcddTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkFlowStepID()) {
		case "JCDD030":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的完结操作
		case "JCDD031":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的退回操作
		case "JCDD032":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的延期操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "JCDD" for 检查督导
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> jcddBaseTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkflowType()) {
		case "JCDD030":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的完结操作
		case "JCDD031":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的退回操作
		case "JCDD032":
			return supervisorManagerServer.finish(stepInfoVO);// 调用检查督导的延期操作
		default:
			return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
		}
	}

	/**
	 * "04" for 设备管理
	 *
	 * @param stepInfoVO
	 * @return
	 */
	private Map<String, Object> sbglTask(StepInfoVO stepInfoVO) {
		switch (stepInfoVO.getWorkflowType()) {

		}
		return null;
	}

}
